BINDIR:= ./bin
OBJDIR:= ./obj
CFLAGS:= $(CFLAGS) -Wall -g3 -O3

_highlight0 = $(shell echo -n "\033[1;36;40m$1\033[0m")
_highlight1 = $(shell echo -n "\033[1;37;40m$1\033[0m")
_highlight2 = $(shell echo -n "\033[1;30;40m$1\033[0m")
_highlight3 = $(shell echo -n "\033[1;35;40m$1\033[0m")
_highlight4 = $(shell echo -n "\033[1;31;40m$1\033[0m")

BIN:= $(shell mkdir -p $(OBJDIR) $(BINDIR)) $(BINDIR)/nowhttpd
SRC:= $(shell echo *.c)
OBJ:= $(addprefix $(OBJDIR)/, $(addsuffix .o, $(basename $(SRC))))
DEP:= $(addprefix $(OBJDIR)/, $(addsuffix .d, $(basename $(SRC))))

all: $(BIN)

$(BIN): $(OBJ)
	@ echo -n -e "$(call _highlight1,"linking...")"
	@ $(CC) $(CFLAGS) -o $@ $^
	@ echo -e "$(call _highlight0,"done.")"

clean:
	@ echo -n -e "$(call _highlight2,"cleaning...")"
	@ rm -rf $(DEP) $(OBJ) $(BIN)
	@ rmdir --ignore-fail-on-non-empty $(OBJDIR) $(BINDIR)
	@ echo -e "$(call _highlight0,"done.")"

-include $(DEP)

$(OBJDIR)/%.d: %.c
	@ echo -n -e "$(call _highlight3,"generating make dependence of $<...")"
	@ $(CC) $(CFLAGS) -MM $< | sed "s?\\(.*\\):?$(OBJDIR)/$(basename $<).o $(basename $<).d :?g" > $@
	@ echo -e "$(call _highlight0,"done.")"

$(OBJDIR)/%.o: %.c
	@ echo -n -e "$(call _highlight4,"compiling $<...")"
	@ $(CC) $(CFLAGS) -c -o $@ $<
	@ echo -e "$(call _highlight0,"done.")"
